package cn.wolfcode.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
  @Autowired
    private IRoleService roleService;
    private IEmployeeService service;
    @Autowired
    public void setService(IEmployeeService service) {
        this.service = service;
    }
    private IDepartmentService departmentService;
@Autowired
    public void setDepartmentService(IDepartmentService departmentService) {
        this.departmentService = departmentService;
    }
    @RequiredPermission(name="员工查询",expression = "employee/list/")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo){
        PageInfo<Employee> pageInfo = service.query(qo);
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments",departments);
        model.addAttribute("PageInfo",pageInfo);
        return "employee/list";
    }
    @RequestMapping("/input")
    public String input(Long id,Model model){
        List<Department> departments = departmentService.selectAll();
        //根据id将单个查到的的数据存入模型中,以便在修改的页面回显
        model.addAttribute("departments",departments);
        List list = roleService.selectAll();
        model.addAttribute("roles",list);
        if (id != null){
            Employee employee = service.selectById(id);
            //现在要将该员工之前有的权限查出来放在作用域中以便回显
            model.addAttribute("employee",employee);
            model.addAttribute("selfPermission",service.selectByEmpId(id));

        }
        return "employee/input";
    }
@RequiredPermission(name="员工删除",expression = "employee/delete/")
  @RequestMapping("/delete")
    public String delete(Long id){
        if (id!=null){
            service.delete(id);
        }
        //这里要重定向重新回到list()里查询所有数据回显,不能在视图里寻找
        return "redirect:/employee/list";
    }
    @RequiredPermission(name="员工修改或增加",expression = "employee/saveOrUpdate/")
@RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Employee employee,Long[] ids){
        if (employee.getId() != null){
            service.update(employee,ids);
        }else {service.save(employee,ids);
        }
       //前面要加一个/因为视图解析对重定向和请求转发不管用
        return "redirect:/employee/list";
    }
@RequestMapping("/exportXls")
public void exportXls(HttpServletResponse response) throws IOException {
    String fileName = new String("员工通讯录.xls".getBytes("UTF-8"), "ISO-8859-1");
    response.setHeader("Content-disposition","attachment;filename="+fileName);
    service.exportXls(response.getOutputStream());
}

    @RequestMapping("/importXls")
    public String importXls(MultipartFile file) throws Exception {
         service.importXls(file);
        return "redirect:/employee/list";
    }
}
